<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;

class IndexShopType extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_type';

    protected static function init()
    {
        //TODO:初始化内容
    }

    /**
     * @var array
     * 查询器
     */
    protected $append = [
        'url_text',
    ];

    /**
     * @param $value
     * @param $data
     * @return mixed
     * 连接地址
     */

    public function getUrlTextAttr($value,$data){
        $url  = '/shop/pages/filter?id='.$data['id'].'&title='.$data['title'];
        return $url;
    }

    /**
     * @param $dis
     * @param $page
     * @return array
     * @throws \think\exception\DbException
     * 获取分类列表
     */
    public function getCatList($dis,$page){
        $data         = $this->where($dis)->order('top desc')->paginate($page)->toArray();
        $data['data'] = transImages($data['data'],['cover'],',');
        $data['data'] = $this->getTree($data['data'],0);
        return $data;
    }

    /**
     * @param $data
     * @param $pId
     * @return array
     * 递归无限极
     */
    public function getTree($data, $pId){
         $tree = array();
        if(!empty($data)){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId) {
                    $v['child'] = $this->getTree($data, $v['id']);
                    $tree[]   = $v;
                }
            }
        }
        return $tree;
    }


    /**
     * @param $dis
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取分类
     */

    public function catSelect($dis){
        $data = $this->where($dis)->order('top desc,id desc')->select()->toArray();
        $data = transImagesOne($data,['cover'],',');
        $data = $this->getTree($data,0);

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-11 16:22
     * @功能说明:diy版本的分类列表
     */
    public function catSelectDiy($dis,$file = '*'){

        $data = $this->where($dis)->field($file)->order('top desc,id desc')->select()->toArray();

        $data = transImagesOne($data,['img'],',');

        if(!empty($data)){

            foreach ($data as &$v){

                $v['link'][] = [
                    //标题
                    'title' => $v['title'],
                    //链接
                    'url'   => $v['url_text']
                ];

                $v['linkType'] = 4;

                $img = $v['img'];

                $v['img'] = [];

                $v['img'][0]['url'] = $img;
            }

        }

        return $data;
    }



    /**
     * @param $id
     * @return mixed
     * 检查是否是一级分类
     */

    public function checkOne($id){
        $pid = $this->where([['id','=',$id]])->value('pid');
        return $pid;
    }

    /**
     *
     * 获取下级分类
     */

    public function getSonCate($id){
        $data = $this->where([['pid','=',$id]])->select()->toArray();
        return $data;
    }


    /**
     * 获取下级分类id
     *
     */

    public function getSonCatId($id){
        $pid    = $this->checkOne($id);
        $son_id = array();
        if($pid==0){
            $son_id = $this->where([['pid','=',$id]])->column('id');
        }
        $son_id[] = $id;
        return $son_id;
    }





}